package com.teemor.tim.utils;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ApplicationEvent;
import org.springframework.stereotype.Component;

import java.io.Serializable;

/**
 * spring 容器上下文
 * 
 * @ename wade.nie
 * @cname 聂敏
 * @email 550552699@qq.com
 * @date 2019年1月28日
 */
@Component
public final class ContextWarer implements ApplicationContextAware, Serializable {

	private static final long serialVersionUID = 1969711427186627089L;

	private static ApplicationContext applicationContext;

	@Override
	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
		ContextWarer.applicationContext = applicationContext;
	}

	public static <T> T getBean(Class<T> clazz) {
		return applicationContext.getBean(clazz);
	}

	@SuppressWarnings("unchecked")
	public static <T> T getBean(String name) {
		return (T) applicationContext.getBean(name);
	}

	public static ApplicationContext getApplicationContext() {
		return applicationContext;
	}


	public void publishEvent(ApplicationEvent applicationEvent) {
		applicationContext.publishEvent(applicationEvent);
	}
}
